package hbase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.master.MasterFileSystem;
import org.apache.hadoop.hbase.master.MasterServices;

import java.io.IOException;

/**
 * Created by spark on 16-10-19.
 */
public class MasterOberserverExample extends BaseMasterObserver{

    private static final Log LOG = LogFactory.getLog(MasterOberserverExample.class);

    @Override
    public void postCreateTable(ObserverContext<MasterCoprocessorEnvironment> ctx, HTableDescriptor desc, HRegionInfo[] regions) throws IOException {

        String tableName = desc.getNameAsString();

        MasterServices services = ctx.getEnvironment().getMasterServices();
        MasterFileSystem masterFileSystem = services.getMasterFileSystem();
        FileSystem fs = masterFileSystem.getFileSystem();
        Path blobPath = new Path(tableName + "-blob");
        fs.mkdirs(blobPath);

        LOG.info("create table blob dir: " + blobPath.toString());
    }
}
